import request from '@renderer/utils/request'
import type { WorkerQueryParams, WorkerListResponse, Worker, WorkerStats } from '@renderer/store/modules/worker'

// API响应类型
interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

export const workerApi = {
  /**
   * 获取打手统计数据
   */
  getWorkerStats(): Promise<ApiResponse<WorkerStats>> {
    return request.get('/api/manager/workers/stats')
  },

  /**
   * 获取打手列表
   */
  getWorkerList(params: WorkerQueryParams): Promise<ApiResponse<WorkerListResponse>> {
    return request.get('/api/manager/workers', { params })
  },

  /**
   * 获取打手详情
   */
  getWorkerDetail(workerId: number): Promise<ApiResponse<Worker>> {
    return request.get(`/api/manager/workers/${workerId}`)
  },

  /**
   * 创建新打手
   */
  createWorker(workerData: Omit<Worker, 'id'>): Promise<ApiResponse<Worker>> {
    return request.post('/api/manager/workers', workerData)
  },

  /**
   * 更新打手信息
   */
  updateWorker(workerId: number, workerData: Partial<Worker>): Promise<ApiResponse<Worker>> {
    return request.put(`/api/manager/workers/${workerId}`, workerData)
  },

  /**
   * 更新打手状态
   */
  updateWorkerStatus(workerId: number, status: string): Promise<ApiResponse<void>> {
    return request.put(`/api/manager/workers/${workerId}/status?status=${status}`)
  },

  /**
   * 删除打手
   */
  deleteWorker(workerId: number): Promise<ApiResponse<void>> {
    return request.delete(`/api/manager/workers/${workerId}`)
  },

  /**
   * 为打手分配订单
   */
  assignOrder(workerId: number, orderId: number): Promise<ApiResponse<void>> {
    return request.post(`/api/manager/workers/${workerId}/assign-order`, { orderId })
  },

  /**
   * 获取打手历史记录
   */
  getWorkerHistory(workerId: number, params?: any): Promise<ApiResponse<any>> {
    return request.get(`/api/manager/workers/${workerId}/history`, { params })
  },

  /**
   * 获取打手业绩报表
   */
  getWorkerPerformance(workerId: number, params?: any): Promise<ApiResponse<any>> {
    return request.get(`/api/manager/workers/${workerId}/performance`, { params })
  },

  /**
   * 暂停/启用打手
   */
  toggleWorkerStatus(workerId: number, isActive: boolean): Promise<ApiResponse<void>> {
    return request.patch(`/api/manager/workers/${workerId}/toggle`, { isActive })
  },

  /**
   * 批量操作打手
   */
  batchUpdateWorkers(workerIds: number[], action: string, data?: any): Promise<ApiResponse<void>> {
    return request.post('/api/manager/workers/batch', {
      workerIds,
      action,
      data
    })
  },

  /**
   * 获取可分配的订单列表
   */
  getAvailableOrders(): Promise<ApiResponse<any>> {
    return request.get('/api/manager/orders/available')
  },

  /**
   * 获取打手当前订单详情
   */
  getWorkerCurrentOrder(workerId: number): Promise<ApiResponse<any>> {
    return request.get(`/api/manager/workers/${workerId}/current-order`)
  },

  /**
   * 搜索打手
   */
  searchWorkers(keyword: string): Promise<ApiResponse<Worker[]>> {
    return request.get('/api/manager/workers/search', {
      params: { keyword }
    })
  },

  /**
   * 导出打手数据
   */
  exportWorkers(params?: any): Promise<ApiResponse<string>> {
    return request.get('/api/manager/workers/export', {
      params,
      responseType: 'blob'
    })
  },

  /**
   * 获取打手技能标签列表
   */
  getWorkerSkills(): Promise<ApiResponse<string[]>> {
    return request.get('/api/manager/workers/skills')
  },

  /**
   * 更新打手技能
   */
  updateWorkerSkills(workerId: number, skills: string[]): Promise<ApiResponse<void>> {
    return request.patch(`/api/manager/workers/${workerId}/skills`, { skills })
  },

  /**
   * 获取打手评分和评价
   */
  getWorkerRatings(workerId: number, params?: any): Promise<ApiResponse<any>> {
    return request.get(`/api/manager/workers/${workerId}/ratings`, { params })
  },

  /**
   * 设置打手分成比例
   */
  setWorkerCommission(workerId: number, commissionRate: number): Promise<ApiResponse<void>> {
    return request.patch(`/api/manager/workers/${workerId}/commission`, { commissionRate })
  }
}